package edu.jhu.tcj.purfuit;

import java.beans.PropertyChangeSupport;
import java.util.Collection;

/**
 * @author Bobby
 * @author Brad Gabrielse
 */
public class Turn {

	public enum TurnState {
		WaitingToRoll,
		WaitingToChooseSpace,
		WaitingToAnswerQuestion,
		TurnOver,
		GameOver;
	}

	public static final String TURN_STATE_UPDATE = "TURN_STATE_UPDATE";
	
	private TurnState turnState;
	
	private Player player;

	private final Game game;

	private Collection<Space> availableSpaces;
	
	private Question currentQuestion;

	private final PropertyChangeSupport pcs;
	
	public Turn(Player player, Game game, PropertyChangeSupport pcs) {
		this.player = player;
		this.game = game;
		this.pcs = pcs;
		setTurnState(TurnState.WaitingToRoll);
	}

	/**
	 * @return the player
	 */
	public Player getPlayer() {
		return player;
	}

	/**
	 * @return the turnState
	 */
	public TurnState getTurnState() {
		return turnState;
	}
	
	private void setTurnState(TurnState turnState) {
		TurnState oldState = this.turnState;
		this.turnState = turnState;
		pcs.firePropertyChange(TURN_STATE_UPDATE, oldState, this.turnState);
	}
	
	public void startPlayersTurn(Player player) {
		int numberRolled = game.rollDie();
		Board board = game.getBoard();
		Space currentSpace = player.getSpace();
		availableSpaces = board.getAvailableSpaces(currentSpace, numberRolled);
		setTurnState(TurnState.WaitingToChooseSpace);
	}
	
	public void movePlayer(Space space){
		game.getBoard().movePlayer(player, space);
		boolean rollAgain = space.isRollAgain();
		boolean centerSpace = space.isCenterSpace();
		if ((rollAgain && centerSpace && player.getCompletedCategories().length != 4) ||
				(rollAgain && !centerSpace)) {
			//TODO improve logic, remove hardcoded 4
			setTurnState(TurnState.WaitingToRoll);
		} else {
			currentQuestion = space.generateQuestion();
			setTurnState(TurnState.WaitingToAnswerQuestion);
		}
		
	}
	
	public void answerQuestion(String answer){
		if (currentQuestion.getCorrectAnswer().equals(answer)) {
			Space playerPosition = player.getSpace();
			if (playerPosition.isHeadQuarter()) {
				player.addCompletedCategories(currentQuestion.getCategory());
			} else if (playerPosition.isCenterSpace()) {
				game.endGame();
				setTurnState(TurnState.GameOver);
				return;
			}
			setTurnState(TurnState.WaitingToRoll);
		} else {
			setTurnState(TurnState.TurnOver);
		}
	}
	
	public Question getCurrentQuestion() {
		return this.currentQuestion;
	}
	
	public Collection<Space> getAvailableSpaces() {
		return this.availableSpaces;
	}
	
	/**
	 * Used to set the Turn object during a load operation
	 * 
	 * @param player
	 * @param spaces
	 * @param question
	 * @param state
	 */
	public void setAfterLoad(Player player, Collection<Space> spaces, 
			Question question, TurnState state){
		this.availableSpaces = spaces;
		this.currentQuestion = question;
		this.player = player;
		this.turnState = state;
	}
}
